package com.qf.ex;

import com.qf.entity.Result;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一处理异常
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestControllerAdvice
public class MyHandelException {

    /**
     * 处理自定义的异常
     * @param e
     * @return
     */
    @ExceptionHandler({
            CustomException.class
    })
    public Result handleCustomException(CustomException e){
        e.printStackTrace();
        return Result.error(e.getMessage());
    }

    /**
     * 处理认证失败，或者没有权限的异常
     */
    @ExceptionHandler({
            UnauthorizedException.class
    })
    public Result handleUnauthorizedException(UnauthorizedException e){
        e.printStackTrace();
        return  new Result(401, "权限不足");
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler({
            Exception.class
    })
    public Result handleOtherException(Exception e){
        return Result.error("系统异常，请联系管理员");
    }
}
